package lp.xian.materials.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

import com.ruoyi.common.domain.SupplierGoods;
import com.ruoyi.common.mapper.SupplierGoodsMapper;
import com.ruoyi.common.utils.DataUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.SecurityUtils;

import lp.xian.materials.domain.Purchase;
import lp.xian.materials.domain.PurchaseTable;
import lp.xian.materials.domain.RequisitionTable;
import lp.xian.materials.mapper.PurchaseMapper;
import lp.xian.materials.mapper.PurchaseTableMapper;
import lp.xian.materials.mapper.RequisitionTableMapper;
import lp.xian.materials.vo.ReqVo;
import lp.xian.materials.vo.RequisitionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.materials.mapper.RequisitionMapper;
import lp.xian.materials.domain.Requisition;
import lp.xian.materials.service.IRequisitionService;

/**
 * 请购计划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-05
 */
@Service
public class RequisitionServiceImpl implements IRequisitionService
{
    @Autowired
    private RequisitionMapper requisitionMapper;
    @Autowired
    private RequisitionTableMapper requisitionTableMapper;
    @Autowired
    private PurchaseTableMapper purchaseTableMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private SupplierGoodsMapper supplierGoodsMapper;

    /**
     * 查询请购计划
     *
     * @param requisitionId 请购计划主键
     * @return 请购计划
     */
    @Override
    public Requisition selectRequisitionByRequisitionId(Integer requisitionId)
    {
        return requisitionMapper.selectRequisitionByRequisitionId(requisitionId);
    }

    /**
     * 查询请购计划列表
     *
     * @param requisition 请购计划
     * @return 请购计划
     */
    @Override
    public List<Requisition> selectRequisitionList(Requisition requisition)
    {
        return requisitionMapper.selectRequisitionList(requisition);
    }

    /**
     * 新增请购计划
     *
     * @param reqVo 请购计划
     * @return 结果
     */
    @Override
    public int insertRequisition(ReqVo reqVo)
    {
        List<SupplierGoods> supplierGoods = reqVo.getListOrder();
        //新增请购计划
        Requisition requisition=new Requisition();
        requisition.setRequisitionPo(reqVo.getRequisitionPo());
        requisition.setRequisitionName(reqVo.getRequisitionName());
        requisition.setRequisitionPerson(reqVo.getRequisitionPerson());
        requisition.setRequisitionDate(reqVo.getRequisitionDate());
        requisition.setSupplierId(reqVo.getSupplierId());
        requisition.setAuditStatus(0);
        requisition.setRequisitionType(0);
        requisition.setCreateBy(SecurityUtils.getUsername());
        requisition.setCreateTime(DateUtils.getNowDate());
        requisitionMapper.insertRequisition(requisition);
        RequisitionTable requisitionTable = new RequisitionTable();
        for (int i = 0; i < supplierGoods.toArray().length; i++) {
            String orderNo=createNo(supplierGoods.get(i).getSupplierTableId().toString());
            requisitionTable.setRequisitionOrderNo(orderNo);
            requisitionTable.setRequisitionId(requisition.getRequisitionId());
            requisitionTable.setRequisitionTableName(reqVo.getSupplierTableName());//姓名
            requisitionTable.setSupplierId(reqVo.getSupplierId());
            requisitionTable.setRequisitionTableSpeion(supplierGoods.get(i).getSupplierGoodsName());
            requisitionTable.setRequisitionTablePrice(supplierGoods.get(i).getSupplierGoodsPrice());
            requisitionTable.setRequisitionTableNum(supplierGoods.get(i).getRequisitionNum());
            requisitionTable.setRequisitionTableUnit(supplierGoods.get(i).getSupplierGoodsWeight());
            requisitionTable.setRequisitionTableTax(reqVo.getRequisitionTableTax());
            BigDecimal GoodsPrice=supplierGoods.get(i).getSupplierGoodsPrice();//价格
            Integer RequisitionNum=supplierGoods.get(i).getRequisitionNum();//数量
            BigDecimal RequisitionTableTax=reqVo.getRequisitionTableTax();//税率
            BigDecimal TaxPrice=GoodsPrice.multiply(new BigDecimal(RequisitionNum)).multiply(RequisitionTableTax).multiply(new BigDecimal(0.01));//税额
            BigDecimal TotalPrice=GoodsPrice.multiply(new BigDecimal(RequisitionNum)).add(TaxPrice);//含税总金额
            requisitionTable.setRequisitionTableTotal(TotalPrice);//含税总金额
            requisitionTable.setRequisitionTableDate(new Date());
            requisitionTableMapper.insertRequisitionTable(requisitionTable);
        }
        SupplierGoods supplierGoods1=new SupplierGoods();
        for (int i = 0; i < supplierGoods.toArray().length; i++) {
            supplierGoods1.setSupplierGoodsId((supplierGoods.get(i).getSupplierGoodsId()));
            Integer GoodsNum=supplierGoods.get(i).getSupplierGoodsNum();
            Integer RequisitionNum=supplierGoods.get(i).getRequisitionNum();
            Integer SupplierGoodsNum=GoodsNum-RequisitionNum;
            supplierGoods1.setSupplierGoodsNum(SupplierGoodsNum);
            supplierGoodsMapper.updateSupplierGoods(supplierGoods1);
        }
        return 1;
    }

    /**
     * 修改请购计划
     *
     * @param requisition 请购计划
     * @return 结果
     */
    @Override
    public int updateRequisition(Requisition requisition)
    {
        requisition.setUpdateTime(DateUtils.getNowDate());
        return requisitionMapper.updateRequisition(requisition);
    }

    /**
     * 批量删除请购计划
     *
     * @param requisitionIds 需要删除的请购计划主键
     * @return 结果
     */
    @Override
    public int deleteRequisitionByRequisitionIds(Integer[] requisitionIds)
    {
        return requisitionMapper.deleteRequisitionByRequisitionIds(requisitionIds);
    }

    /**
     * 删除请购计划信息
     *
     * @param requisitionId 请购计划主键
     * @return 结果
     */
    @Override
    public int deleteRequisitionByRequisitionId(Integer requisitionId)
    {
        return requisitionMapper.deleteRequisitionByRequisitionId(requisitionId);
    }

    @Override
    public int auditRequisition(RequisitionVo requisitionVo)throws ParseException {
        List<RequisitionTable> purchaseTables = requisitionVo.getOrderList();
        Requisition requisition=new Requisition();
        for (int i = 0; i < purchaseTables.toArray().length; i++) {
            requisition.setRequisitionId(purchaseTables.get(i).getRequisitionId());
            requisition.setAuditStatus(1);
            requisition.setAuditTime(new Date());
            requisition.setAuditPerson(SecurityUtils.getUsername());
        }
        requisitionMapper.updateRequisition(requisition);

        if(requisition.getAuditStatus().intValue()==1) {
            Purchase purchase=new Purchase();
                String orderNo=createNo(requisitionVo.getRequisitionIds().toString());
                purchase.setPurchaseOrder(orderNo);
                purchase.setSupplierId(requisitionVo.getSupplierIds());//供应商
                purchase.setPurchasePerson(requisitionVo.getPurchasePerson());//业务员
                purchase.setAuditStatus(0);//审核状态
                purchase.setRequisitionTableTax(requisitionVo.getRequisitionTableTax());//税率
                purchase.setPurchaseDate(requisitionVo.getPurchaseDate());//日期
                purchase.setCreateBy(SecurityUtils.getUsername());
                purchase.setCreateTime(new Date());
                purchaseMapper.insertPurchase(purchase);

           PurchaseTable purchaseTable = new PurchaseTable();
            for (int k = 0; k < purchaseTables.toArray().length; k++) {
                purchaseTable.setPurchaseId(purchase.getPurchaseId());
                purchaseTable.setPurchaseOrderNo(purchaseTables.get(k).getRequisitionOrderNo());
                purchaseTable.setPurchaseTableName(purchaseTables.get(k).getRequisitionTableName());
                purchaseTable.setPurchaseTableSpeion(purchaseTables.get(k).getRequisitionTableSpeion());//规格型号
                purchaseTable.setPurchaseTableNum(purchaseTables.get(k).getRequisitionTableNum());
                purchaseTable.setPurchaseTableUnit(purchaseTables.get(k).getRequisitionTableUnit());//单位
                purchaseTable.setPurchasePrice(purchaseTables.get(k).getRequisitionTablePrice());//单价
                purchaseTable.setPurchaseTotalPrice(purchaseTables.get(k).getRequisitionTableTotal());//总价
                purchaseTable.setPurchaseTax(purchaseTables.get(k).getRequisitionTableTax());//税率
                purchaseTable.setPurchaseDate(purchaseTables.get(k).getRequisitionTableDate());//日期
                //含税
                BigDecimal RequisitionTablePrice=purchaseTables.get(k).getRequisitionTablePrice();//单价
                Integer RequisitionTableNum=purchaseTables.get(k).getRequisitionTableNum();//数量
                BigDecimal RequisitionTableTax=purchaseTables.get(k).getRequisitionTableTax();//税率
                BigDecimal TaxPrice=RequisitionTablePrice.multiply(new BigDecimal(RequisitionTableNum)).multiply(RequisitionTableTax).multiply(new BigDecimal(0.01));//税额
                BigDecimal TotalPrice=RequisitionTablePrice.multiply(new BigDecimal(RequisitionTableNum)).add(TaxPrice);
                BigDecimal UnitPrice=TotalPrice.divide(new BigDecimal(RequisitionTableNum));
                purchaseTable.setPurchaseUnitPrice(UnitPrice);//含税单价
                purchaseTable.setPurchaseUnitTotalPrice(TotalPrice);//含税总金额
                purchaseTable.setPurchaseTaxPrice(TaxPrice);//税额
                purchaseTableMapper.insertPurchaseTable(purchaseTable);
            }
        }
        return 1;
    }

    public String createNo(String id){
        String no="";
        StringBuffer buffer=new StringBuffer(0000);
        buffer.append(MathUtils.random());
        if (id.length()<3){
            for(int i=3-id.length();i>0;i--){
                buffer.append("0");
            }
        }
        no=buffer.append(id).toString();
        Map<String,Object> params=new HashMap<String, Object>();
        return no;
    }
}
